<?php

include("global.php");

if($loggingin==1&&$login==1){
	
	$loggingin=0;
	
	$pagetitle=" ".template('pagetitleseparator')." Logged in";
	$links=" ".template('linkbarseparator')." Logged in";
	
	include("header.php");
	
	echo template('noticeloggedin',array('link'=>$HTTP_REFERER));
	
	echo template('refresh',array('url'=>$HTTP_REFERER));
}elseif($logout==1){
	
	$pagetitle=" ".template('pagetitleseparator')." Logged out";
	$links=" ".template('linkbarseparator')." Logged out";
	
	include("header.php");
	
	echo template('noticeloggedout',array('link'=>"index.php"));

	echo template('refresh',array('url'=>'index.php'));
}else{
	
	if($configarray[40]){
		include("header.php");
		
		echo template('noticeboardclosed',array('reason'=>$configarray[40]));
	}else{
		
		#########################################
		###############SHOW FORUMS####################
		if(!isset($forum)){
			
			$pagetitle="";
			$links="";
			
			include("header.php");
			
			subforums("0",-1);
			
			if($userloggedinoptarray[1]!=="on"){//if hide icon guide is not selected in user options
				$tplcontent['iconguide']=template("forumiconguide");
			}
			
			echo template('forumview',$tplcontent);
			
		}
		//END SHOW FORUMS
		
		#########################################
		##############SHOW TOPICS################
		if(isset($forum)&&!isset($topic)){
			
			$topictimearray=listdirs("$configarray[2]/$forum");
			$forumconfigarray=getdata("$configarray[2]/$forum.php");
			
			if(count($forumconfigarray)>0){
				
				$pagetitle=" ".template('pagetitleseparator')." $forumconfigarray[3]";
				
				//start link bar
				$links=" ".template('linkbarseparator')." $forumconfigarray[3]";
				
				$forumconfigarray2=getdata("$configarray[2]/$forum.php");
				
				while($forumconfigarray2[4]!=="0"){
					$links2=" ".template('linkbarseparator')." <a href=\"index.php?forum=$forumconfigarray2[4]\">";
					$forumconfigarray2=getdata("$configarray[2]/$forumconfigarray2[4].php");
					$links="$links2$forumconfigarray2[3]</a>$links";
				}
				
				//end link bar
				
				include("header.php");
				
				if(!getforumaccess("view")){
					echo template('noticeforumnoview');
				}else{
					
					$subforums=0;
					
					for($n=0;$n<count($forumarray);$n++){
						$forumconfigarray4=getdata("$configarray[2]/$forumarray[$n].php");
						
						if($forumconfigarray4[4]=="$forum"){
							$subforums=1;
							break 1;
						}
						
					}
					
					if($subforums==1){
						
						subforums("$forum",-1);
						$forumarray=listdirs("$configarray[2]");
						
						if($userloggedinoptarray[1]!=="on"){//if hide icon guide is not selected in user options
							$tplcontent['iconguide']=template("forumiconguide");
						}
						
						echo template('forumview',$tplcontent);
					}
					
					$forumconfigarray3=getdata("$configarray[2]/$forum.php");
					
					if($forumconfigarray3[5]!=="category"){
						
						$tplcontent['topiclistbuttons']=topiclistbuttons();
						
						if(!$page){$page=1;}
						
						@rsort($topictimearray);
						
						for($n=(($page-1)*$configarray[7]);$n<count($topictimearray)&&$n<((($page-1)*$configarray[7])+($configarray[7]));$n++){
							unset($tplcontent2);
							$postarray=listfiles("$configarray[2]/$forum/$topictimearray[$n]");
							$firstpostarray=getdata("$configarray[2]/$forum/$topictimearray[$n]/0.php");
							@rsort($postarray);
							$lastpostarray=getdata("$configarray[2]/$forum/$topictimearray[$n]/$postarray[0].php");
							@sort($postarray);
							$topicconfigarray=getdata("$configarray[2]/$forum/$topictimearray[$n].php");
							$topicnum=topic_timetonum($forum,$topictimearray[$n]);
							
							$lastposton=$lastpostarray[1];
							$lastloginarray=explode("\t",$userloggedinarray[5]);
							
							if($topicconfigarray[0]=="closed"){
								$tplcontent2['icon'].="<img src=\"$theme_imagespath/locked.gif\" alt=\"\">";
							}else{
								if($lastposton>$lastloginarray[0]){
									$tplcontent2['icon'].="<img src=\"$theme_imagespath/new.gif\" alt=\"\">";
								}else{
									$tplcontent2['icon'].="<img src=\"$theme_imagespath/nonew.gif\" alt=\"\">";
								}
							}
							
							$stickytest=substr($topictimearray[$n],0,10);
							
							if($stickytest=="important_"){
								$tplcontent2['topicname'].=template("postsubjectimportant");
							}
							
							$extraarray=explode("\t",$firstpostarray[4]);
							if($extraarray[0]=="poll"){
								$tplcontent2['topicname'].=template("postsubjectpoll");
							}
							
							$firstpostarray[2]=htmlentities($firstpostarray[2]);
							
							$tplcontent2['topicname'].="<a href=\"index.php?forum=$forum&topic=$topicnum\">$firstpostarray[2]</a>";
							
							//show timestamp
							$useroptarray=getdata($configarray[1]."/".$useridarray[$navboardlogin]."/options.php");
							if($useroptarray[4]=="on"){
								$tplcontent2['topicname'].=" (".$topictimearray[$n].")";
							}
							
							if(count($postarray)>$configarray[8]){
								$page2=1;
								
								$tplcontent2['pages'].=template('page');
								for($m=0;$m<count($postarray);$m+=$configarray[8]){
									$tplcontent2['pages'].="<a href=\"index.php?forum=$forum&topic=$topicnum&page=$page2\">$page2</a> ";
									$page2++;
								}
								
							}
							
							$userarray=getdata("$configarray[1]/$firstpostarray[0]/main.php");
							
							if(count($userarray)>0&&$firstpostarray[0]){
								$tplcontent2['starter'].="<a href=\"profile.php?user=$firstpostarray[0]\">$userarray[0]</a>";
							}else{
								$tplcontent2['starter'].=template('guest');
							}
							
							$posts=count($postarray)-1;
							$tplcontent2['posts'].="$posts";
							
							if($topicconfigarray[2]){
								$tplcontent2['views'].="$topicconfigarray[2]";
							}else{
								$tplcontent2['views'].="0";
							}
							
							//rating
							$ratingarray=explode("\t",$threadconfigarray[1]);
							if($ratingarray[0]>0&&$ratingarray[1]>0){
								$rating=$ratingarray[0]/$ratingarray[1];
								$rating=round($rating,1);
								$tplcontent2['rating'].=$rating.'/10';
							}else{
								$tplcontent2['rating'].='&nbsp;';
							}
							
							$lastpostby=$lastpostarray[0];
							$lastposton=date($dateformat,$lastpostarray[1]+$timeoffsetseconds);
							
							unset($tplcontent3);
							$tplcontent3['date'].=$lastposton;
							
							$userarray=getdata("$configarray[1]/$lastpostby/main.php");
							if(count($userarray)>0){
								$tplcontent3['user'].="<a href=\"profile.php?user=$lastpostby\">$userarray[0]</a>";
							}else{
								$tplcontent3['user'].=template('guest');
							}
							
							$tplcontent2['lastpost'].=template('topiclastpost',$tplcontent3);
							
							$tplcontent['topicrows'].=template('topicrow',$tplcontent2);
							
						}
						
						if($userloggedinoptarray[1]!=="on"){
							$tplcontent['iconguide']=template('topiciconguide');
						}
						
						$tplcontent['pages'].=template('page');
						$page2=1;
						
						for($n=0;$n<count($topictimearray);$n+=$configarray[7]){
							
							if($n==(($page-1)*$configarray[7])) {
								$tplcontent['pages'].="<b>$page2</b> ";
							}else{
								$tplcontent['pages'].="<a href=\"index.php?forum=$forum&page=$page2\">$page2</a> ";
							}
							
							$page2++;
						}
						
						echo template('topicview',$tplcontent);
						
					}//end check if category
					
				}//view access check
				
			}else{
				
				$pagetitle=template('pagetitleseparator').template('forumviewtitle');
				
				//start link bar
				$links=template('linkbarseparator').template('forumviewtitle');
				
				include("header.php");
				
				echo template('noticeforumnotexist');
				
			}//end no such forum bracket
			
		}//END SHOWTHREADS
		
		#########################################
		####################SHOW POSTS######################
		
		if(isset($forum)&&isset($topic)){
			
			$topicnum=$topic;
			$topictime=topic_numtotime($forum,$topic);
			
			//$topicarray=listdirs("$configarray[2]/$forum");
			$forumconfigarray=getdata("$configarray[2]/$forum.php");
			$topicconfigarray=getdata("$configarray[2]/$forum/$topictime.php");
			$postarray=listfiles("$configarray[2]/$forum/$topictime");
			@sort($postarray,SORT_NUMERIC);
			$firstpostarray=getdata("$configarray[2]/$forum/$topictime/0.php");
			$userlevelsarray=getdata("$configarray[5]/userlevels.php");
			$moderatorsarray=explode(",",$forumconfigarray[1]);
			
			if(count($postarray)>0){
				
				$pagetitle=template('pagetitleseparator')."$forumconfigarray[3]".template('pagetitleseparator')."$firstpostarray[2]";
				
				//start link bar
				$links=template('linkbarseparator')."<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>";
				
				$forumconfigarray2=getdata("$configarray[2]/$forum.php");
				
				while($forumconfigarray2[4]!=="0"){
					$links2=template('linkbarseparator')."<a href=\"index.php?forum=$forumconfigarray2[4]\">";
					$forumconfigarray2=getdata("$configarray[2]/$forumconfigarray2[4].php");
					$links=$links2.$forumconfigarray2[3]."</a>".$links;
				}
				
				$firstpostarray[2]=htmlentities($firstpostarray[2]);
				
				if(substr($topictime,0,10)=="important_"){
					$links=$links.template('linkbarseparator').$languagedef[316].": ".$firstpostarray[2];
				}else{
					$links=$links.template('linkbarseparator').$firstpostarray[2];
				}
				//end link bar
				
			}else{
				$pagetitle=template('pagetitleseparator').template('topicviewtitle');
				$links=template('linkbarseparator').template('topicviewtitle');
			}//exist check
			
			include("header.php");
			
			if(!getforumaccess("view")){
				echo template('noticetopicnoview');
			}else{
				
				if(count($postarray)<=0){
					echo template('noticetopicnotexist');
					tableheader1();
				}else{
					
					writedata("$configarray[2]/$forum/$topictime.php",$topicconfigarray[2]+1,2);
					
					if(!$page){$page=1;}
					if($page=="last"){
						$num=count($postarray)/$configarray[8];
						if($num==intval($num)){$page=$num;}else{$page=intval($num)+1;}
					}
					
					for($n=(($page-1)*$configarray[8]);$n<count($postarray)&&$n<((($page-1)*$configarray[8])+($configarray[8]));$n++)
					{
						$tplcontent['postrows'].=displaypostrow($forum,$topictime,$postarray[$n]);
					}
					
					$tplcontent['topicbuttons']=topicbuttons();
					
					// if user is administrator or moderator and is not a guest
					if($userloggedinarray[15]=="administrator"||(in_array($useridarray[$navboardlogin],$moderatorsarray)&&!$userloggedinarray[0]=="")){
						
						unset($tplcontent2);
						
						$tplcontent2['adminoptionsformheader']="<form action=\"admin_topicoptions.php\" method=get>
						<input type=hidden name=\"forum\" value=\"$forum\" size=40>
   						<input type=hidden name=\"topic\" value=\"$topicnum\" size=40>";
						
						$tplcontent2['adminoptions'].="<select size=1 name=\"action\" class=\"forminput\">";
						
						//if topic not closed / is open
						if($topicconfigarray[0]==""||$topicconfigarray[0]=="open") 
						{
							$tplcontent2['adminoptions'].=template('postadminoptionclose',array('value'=>'closetopic'));
						}
						//if topic is not open / is closed
						if($topicconfigarray[0]=="closed") 
						{
							$tplcontent2['adminoptions'].=template('postadminoptionopen',array('value'=>'opentopic'));
						}
						
						$tplcontent2['adminoptions'].=template('postadminoptionmove',array('value'=>'movetopic'));
						
						$tplcontent2['adminoptions'].="</select>";
						
						$tplcontent2['adminoptionssubmitbutton'].="
   						<input type=submit name=\"submit\" value=\"".$languagedef['submit']."\" class=\"formbutton\">";
						
						$tplcontent2['adminoptionsformfooter']="</form>";
						
						$tplcontent['adminoptions'].=template('postadminoptions',$tplcontent2);
					}else{
						$tplcontent['adminoptions'].="&nbsp;";
					}
					$page2=1;
					
					for($n=0;$n<count($postarray);$n+=$configarray[8]){
						
						if($n==(($page-1)*$configarray[8])){
							$tplcontent['pages'].="<b>$page</b> \n";
						}else{
							$tplcontent['pages'].="<a href=\"index.php?forum=$forum&topic=$topicnum&page=$page2";
							$tplcontent['pages'].="\">$page2</a> ";
						}
						
						$page2++;
					}
					
					echo template('postview',$tplcontent);
					
				}//access test
				
			}//end testing for empty thread bracket
			
		}//show posts check
		
	}//closed check
	
}//logging in check
include("footer.php");
?>
